package pl.vhcreator.domain;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class VirtualWriter 
{
    
    private String pathToVirtual = "";
    
    private ArrayList<String> messages = new ArrayList<String>();
    
    private String address = null;
    
    private String hostName = null;
    
    private String subDirectoryName = null;
    
    private String pathToProjects = "/";
    
    public void setAddress(String address)
    {
        this.address = address;
    }
    
    public String getAddress() 
    {
        return address;
    }
    
    public void setHostName(String hostName) 
    {
        this.hostName = hostName;
    }
    
    public String getHostName() 
    {
        return hostName;
    }
    
    public void setPathToProjects(String pathToProjects) 
    {
        this.pathToProjects = pathToProjects;
    }
    
    public String getPathToProjects()
    {
        return pathToProjects;
    }
    
    public void setSubDirectoryName(String subDirectoryName) 
    {
        this.subDirectoryName = subDirectoryName;
    }
    
    public String getSubDirectoryName()
    {
        return subDirectoryName;
    }
    
    public void addMessage(String msg) 
    {
        this.messages.add(msg);
    }
    
    public void setMessage(String msg)
    {
        this.messages = new ArrayList<String>();
        this.messages.add(msg);
    }
    
    public List<String> getMessage() 
    {
        return messages;
    }
    
    public void setPathToVirtual(String pathToVirtual) 
    {
        this.pathToVirtual = pathToVirtual;
    }
    
    public String getPathToVirtual() 
    {
        return this.pathToVirtual;
    }
    
    public boolean writeNewVirtual() throws NullPointerException
    {
        if (null == hostName) {
            throw new NullPointerException("Host name is required");
        }
        
        if (null == address) {
            throw new NullPointerException("Host IP address is required");
        }
            
        File isFile = new File(getPathToVirtual());
        if (! isFile.exists()) {
            addMessage("Apache vhosts file don't exist !");
            return false;
        }
        
        String projectDirectory = pathToProjects + hostName;
        if (subDirectoryName != null) { 
            projectDirectory += "/" + subDirectoryName;
        }
        
        String template = "\n\nNamevirtualHost %s\n" 
            + "<VirtualHost %s>\n" 
            + "\tDocumentRoot %s\n" 
            + "\tServerAlias %s\n" 
            + "</VirtualHost>\n";
        
        String virtualPrepared = String.format(template, address, address, projectDirectory, hostName);
        try {
            FileWriter fileWriter = new FileWriter(pathToVirtual, true);
            addMessage("Add new virtual host: " + projectDirectory + " " + address);
            fileWriter.append(virtualPrepared);
            fileWriter.close();
            
        } catch (IOException e) {
            addMessage(e.getMessage());
        }
        System.out.println(virtualPrepared);
        return true;
    }
}
